<?php
/**
 * Created by PhpStorm.
 * Author: 芸签cms www.yunqiancms.com
 * User: sun
 * Date: 2022/7/5
 * Time: 8:04 AM
 */
namespace app\backend\modules\business\services;

use app\common\models\AdminUser;

class AdminUserService
{
    /**
     * 获取列表
     * @param $params
     * @return mixed
     */
    public function getList($params){
        $query = AdminUser::select(['uid','username','status','created_at']);
        if ($params['keyword']) {
            $query = $query->whereHas('userProfile', function ($query) use ($params) {
                return $query->select('uid', 'realname')
                    ->where('realname', 'like', '%' . $params['keyword'] . '%');
            })->orWhere('username', 'like', '%' . $params['keyword'] . '%');
        }
        if ($params['department']) {
            $query = $query->whereHas('userProfile', function ($query) use ($params) {
                return $query->whereHas('departmentAdminUsers', function ($query) use ($params) {
                    return $query->whereHas('department', function ($query) use ($params) {
                        return $query->select('id', 'name')
                            ->where('name', 'like', '%' . $params['department'] . '%');
                    });
                });
            });
        }
        if ($params['mobile']) {
            $query = $query->whereHas('userProfile', function ($query) use ($params) {
                return $query->select('uid', 'mobile')
                    ->where('mobile', 'like', '%' . $params['mobile'] . '%');
            });
        }
        if ($params['status']) {
            $query = $query->where('status', $params['status'])
            ;
        }
        $list = $query
            ->with(['userProfile' => function ($profile) {
                return $profile->select('uid', 'realname', 'mobile')
                    ->with(['departmentAdminUsers' => function ($role) {
                        return $role->select()
                            ->with(['department' => function ($role) {
                                return $role->select('id', 'name');
                            }]);
                    }]);
            }])
            ->with(['userRole' => function ($role) {
                return $role->select()
                    ->with(['role' => function ($role) {
                        return $role->select('id', 'name');
                    }]);
            }])
            ->paginate(10);
        $list->map(function ($v){
            $v->realname = $v->userProfile->realname;
            $v->mobile = $v->userProfile->mobile;
            $v->role_name = $v->userRole->role->name;
            $departments = [];
            foreach ($v->userProfile->departmentAdminUsers as $val){
                array_push($departments,$val['department']['name']);
            }
            $v->departments = implode(',',$departments);
            $v->status_desc = AdminUser::$status_map[$v->status];
            unset($v->userProfile);
        });
        return $list;
    }
}